const path = require('path')
const fs = require('fs')
const parse = require('node-html-parser').parse
const pathName = path.join(__dirname, '../dist/index.html')
fs.readFile(pathName, 'utf8', function (err, html) {
	if (err) {
		return console.log('读取index.html文件失败' + err.message)
	}
	const root = parse(html)
	const elList = root.querySelectorAll('script')

	for (let i = 0; i < elList.length; i++) {
		const data = elList[i].getAttribute('type')
		if (data && data === 'module') {
			elList[i].remove()
		}
		const hasNoModule = elList[i].hasAttribute('nomodule')
		if (hasNoModule) {
			elList[i].removeAttribute('nomodule')
		}
		const hasCrossorigin = elList[i].hasAttribute('crossorigin')
		if (hasCrossorigin) {
			elList[i].removeAttribute('crossorigin')
		}
		const hasDataSrc = elList[i].hasAttribute('data-src')
		if (hasDataSrc) {
			const legacyEle = elList[i]
			const srcData = legacyEle.getAttribute('data-src')
			legacyEle.setAttribute('src', srcData)
			legacyEle.removeAttribute('data-src')
			legacyEle.innerHTML = ''
		}
	}
	fs.writeFileSync(pathName, root.toString())
})
